package com.aat.dxfy.shiro;

import java.util.Collection;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ShiroTest01 {
	private static Logger log = LoggerFactory.getLogger(ShiroTest01.class);

	// 根据session获得用户的名称
	public static String principal(Session session) {
		PrincipalCollection principalCollection = (PrincipalCollection) session
				.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
		Object o = principalCollection.getPrimaryPrincipal();
		if (null == o) {
			return "";
		}
		return (String) o;
	}

	public static void login(String u) {
		Subject currentUser = SecurityUtils.getSubject();
		// 5.给他一个会话
		Session session = currentUser.getSession();
		session.setAttribute("key", "Value");
		// 6.当前用户有没有被验证是可用的，如果没有证明或者验证 ，用户密码器执行登录验证
		if (!currentUser.isAuthenticated()) {
			UsernamePasswordToken token = new UsernamePasswordToken(u, "123456");
			token.setRememberMe(true);
			// currentUser.login(token);
			// 7.如果登录失败了？异常会提示什么原因
			try {
				currentUser.login(token);
			} catch (UnknownAccountException e) {
				log.info("没有这个用户！");
			} catch (IncorrectCredentialsException e) {
				log.info("密码错误！");
			} catch (LockedAccountException e) {
				log.info("账户被锁定！");
			} catch (AuthenticationException e) {
				log.info("未知的原因！");
			}
			log.info("User [" + currentUser.getPrincipal()
					+ "] logined in successfully.");
		}
	}

	public static void main(String[] args) {
		org.apache.shiro.web.mgt.DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		org.apache.shiro.web.session.mgt.DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
		org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO sessionDao = new EnterpriseCacheSessionDAO();
		sessionManager.setSessionDAO(sessionDao);
		securityManager.setSessionManager(sessionManager);
		securityManager.setRealm(new MyRealm1());
		// SecurityManager securityManager = fac.getInstance();
		// 3.安全工具里放入安全管理器
		SecurityUtils.setSecurityManager(securityManager);
		// 4.安全工具取得用户或者当前的执行者
		//login("admin");
		//login("admin");
		login("user");
		Collection<Session> sessions = sessionDao.getActiveSessions();
		for (Session s : sessions) {
			System.out.println("1:" + s.toString());
			System.out.println("2:" + principal(s));
		}
		System.exit(0);
	}
	
	
}
